<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="所属分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <select-category v-model="model.categoryId" :select-flag="true"
                               :placeholder="'请选择所属分类'"></select-category>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="属性名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attrName">
              <a-input v-model="model.attrName" placeholder="请输入属性名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否检索" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="searchType">
              <z-switch v-model="model.searchType" :checked="'开'" :un-checked="'关'"></z-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="值类型" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="valueType">
              <z-switch v-model="model.valueType" :left-text="'允许单个值'" :right-text="'允许多个值'"
                        :text-visible="true"></z-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="属性值" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="valueSelectList" v-if="model.valueType == 1">
              <a-select mode="tags" style="width: 100%" placeholder="请输入可选属性值" v-model="model.valueSelectList">
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.valueType == 0">
            <a-form-model-item label="属性值" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="valueSelectListValue">
              <a-input style="width: 100%" placeholder="请输入属性值" v-model="model.valueSelectListValue"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.attrType == 1">
            <a-form-model-item label="所属分组" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="groupId">
              <a-select v-model="model.groupId" placeholder="请选择所属分组">
                <a-select-option v-for="item in groupList" :key="item.id">
                  {{ item.attrGroupName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="属性类型" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="attrType">
              <a-radio-group v-model="model.attrType">
                <a-radio :value="0">
                  销售属性
                </a-radio>
                <a-radio :value="1">
                  基本属性
                </a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="启用状态" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="enable">
              <z-switch v-model="model.enable" :checked="'开'" :un-checked="'关'"></z-switch>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="快速展示" :labelCol="labelCol"
                               :wrapperCol="wrapperCol" prop="showDesc">
              <z-switch v-model="model.showDesc" :checked="'开'" :un-checked="'关'"></z-switch>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction } from '@/api/manage'
import ZSwitch from '../../../components/compents/ZSwitch.vue'
import selectCategory from '../../../components/compents/SelectCategory.vue'
import { getAction, postAction } from '../../../api/manage'

export default {
  name: 'PmsAttrForm',
  components: { ZSwitch, selectCategory },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  watch: {
    'model.categoryId'(newVal) {
      this.initGroupList(newVal)
    }
  },
  data() {
    return {
      model: {
        attrName: undefined,
        attrType: 0,
        categoryId: undefined,
        enable: 0,
        searchType: 0,
        showDesc: 0,
        valueSelectList: [],
        valueType: 0,
        groupId: undefined,
        valueSelectListValue: ''
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        attrName: [
          { required: true, validator: this.verifyAttrName }
        ],
        searchType: [
          { required: true, message: '请选择是否需要检索!' }
        ],
        valueType: [
          { required: true, message: '请选择输入值类型!' }
        ],
        valueSelectList: [
          { required: true, message: '请选择属性可选值!' }
        ],
        attrType: [
          { required: true, message: '请选择属性类型!' }
        ],
        enable: [
          { required: true, message: '请选择启用状态!' }
        ],
        categoryId: [
          { required: true, message: '请选择所属分类!' }
        ],
        showDesc: [
          { required: true, message: '请选择是否快速展示！' }
        ],
        groupId: [
          { required: true, message: '请选择所属分组！' }
        ],
        valueSelectListValue: [
          { required: true, message: '请输入属性值！' }
        ]
      },
      groupList: [],
      url: {
        add: '/product/attribute/add',
        edit: '/product/attribute/edit',
        queryById: '/product/attribute/queryById',
        initGroupList: '/product/attrGroup/initGroupList',
        verifyAttrName: '/product/attribute/verifyAttrName'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {

  },
  methods: {
    /**
     * 初始化分组列表
     */
    initGroupList(categoryId) {
      getAction(this.url.initGroupList, { categoryId: categoryId }).then(res => {
        if (res.success) {
          this.groupList = res.result
        } else {
          this.$message.error('获取当前分类分组失败!')
        }
      })
    },
    add(type) {
      this.model.attrType = type
    },
    edit(record) {
      this.confirmLoading = true
      this.model = Object.assign({}, record)
      getAction(this.url.queryById, { id: record.id }).then(res => {
        if (res.success) {
          this.model = res.result
          this.visible = true
          if (this.model.valueType == 0) {
            this.valueSelectListValue = this.model.valueSelectList[0]
          }
        } else {
          this.$message.error('获取详情信息错误!')
        }
      }).finally(() => {
        this.confirmLoading = false
      })
    },
    submitForm() {
      const that = this
      if (this.model.valueType == 0) {
        this.model.valueSelectList = [this.model.valueSelectListValue]
      }
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    handleChange(value) {
    },
    /**
     * 校验属性名称是否重复
     */
    verifyAttrName(rule, value, callback) {
      if (!this.model.categoryId) {
        return callback(new Error('请先选择所属商品分类!'))
      }
      if (value === undefined || value === '') {
        return callback(new Error('请输入属性名称!'))
      }
      postAction(this.url.verifyAttrName, this.model).then(res => {
        if (res.success) {
          callback()
        } else {
          return callback(new Error(res.message))
        }
      })

    }
  }
}
</script>